//
//  OREditorViewController.m
//  NewsWith
//
//  Created by rimi on 2016/12/13.
//  Copyright © 2016年 dongfumei. All rights reserved.
//

#import "OREditorViewController.h"

@interface OREditorViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UIButton *sexButton;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation OREditorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
}

- (void)initUI{
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveEvent)];

    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.nameTextField];
    [self.tableView addSubview:self.sexButton];
    
}

#pragma mark - UIAlertController

- (void)alertWithTitle:(NSString *)title Message:(NSString *)message buttonBlock:(void(^)(void))block {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    AVUser *user = [AVUser currentUser];
    [alert addAction:[UIAlertAction actionWithTitle:@"男" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [ORUserSingleton sharedUserInfo].accounsex = @"男";
        [_sexButton setTitle:@"男" forState:UIControlStateNormal];
        NSString *sex = [ORUserSingleton sharedUserInfo].accounsex;
        [user setObject:sex forKey:@"sex"];
        [user saveInBackground];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"女" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [ORUserSingleton sharedUserInfo].accounsex = @"女";
        [_sexButton setTitle:@"女" forState:UIControlStateNormal];
        NSString *sex = [ORUserSingleton sharedUserInfo].accounsex;
        [user setObject:sex forKey:@"sex"];
        [user saveInBackground];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
    
}

#pragma mark - @selector

- (void)saveEvent{
    
    [ORUserSingleton sharedUserInfo].accountName = _nameTextField.text;
    AVUser *user = [AVUser currentUser];
    NSString *name = [ORUserSingleton sharedUserInfo].accountName;
    [user setObject:name forKey:@"name"];
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        if (error) {
            return ;
        }
    }];
    [[ORUserSingleton sharedUserInfo] save];
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)sexButtonEvent{
    
    [self alertWithTitle:@"" Message:@"请选择性别" buttonBlock:nil];
    
}

#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *reuseIdentifier = @"OREditorViewController";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"昵称:";
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"性别:";
        
    }
    
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    return cell;
}

#pragma mark - getter

- (UITextField *)nameTextField{
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc]initWithFrame:AAdaptionRect(80, 0, kBaseWidth - 100, 44/AAdaptationWidth())];
        _nameTextField.delegate = self;
        _nameTextField.text = [ORUserSingleton sharedUserInfo].accountName;
        _nameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _nameTextField;
}

- (UIButton *)sexButton{
    if (!_sexButton) {
        _sexButton = [[UIButton alloc]initWithFrame:AAdaptionRect(80, CGRectGetMaxY(self.nameTextField.frame)/AAdaptationWidth(), 50, 44/AAdaptationWidth())];
        [_sexButton setTitle:[ORUserSingleton sharedUserInfo].accounsex forState:UIControlStateNormal];
        [_sexButton setTitleColor:[UIColor blackColor]];
        [_sexButton addTarget:self action:@selector(sexButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sexButton;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:AAdaptionRect(0, 0, kBaseWidth, kBaseHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

@end
